package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Common.Enum.UserType;
import com.weixin.project.Entity.User;
import com.weixin.project.Service.PracticeService;
import com.weixin.project.Service.UserService;
import com.weixin.project.Util.ResponseUtil;
import com.weixin.project.Util.WeChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

@RestController
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    PracticeService practiceService;
    @GetMapping("/login/visitor")
    JSONObject visitorLogin(){
        User visitor = userService.visitor();
        userService.login(visitor);
        return ResponseUtil.success();
    }

    @GetMapping("/login")
    JSONObject login(@RequestParam("code") String code){
        System.out.println("user login......");
        JSONObject ret = new JSONObject();
        JSONObject obj = WeChatUtil.getSessionKeyOrOpenId(code);
        String openId = obj.getString("openid");
        User user = userService.findUserByOpenId(openId);
        //用户第一次登录平台，存储信息
        if (user == null){
            User newUser = new User();
            newUser.setOpenId(openId);
            String userId = UUID.randomUUID().toString();
            newUser.setUserId(userId);
            newUser.setLogin(0);
            newUser.setImage("cloud://practice408-9gnudcyfd6a52382.7072-practice408-9gnudcyfd6a52382-1316931860/img/1684739306867.jpg");
            userService.storeUser(newUser);
            userService.login(newUser);
            ret.put("user_id",userId);
            ret.put("is_admin",user.getType().equals(UserType.ADMIN));
            ret.put("success", true);
        } else {
            userService.login(user);
            ret.put("user_id", user.getUserId());
            ret.put("is_admin",user.getType().equals(UserType.ADMIN));
            ret.put("success", true);
        }
        return ret;
    }

    @GetMapping("/user/get_user_info")
    JSONObject getUserInfo(HttpServletRequest request){
        User currentUser = userService.findCurrentUser(request);
        JSONObject ret = new JSONObject();
        ret.put("user_name", currentUser.getName());
        ret.put("user_img", currentUser.getImage());
        return ResponseUtil.success("", ret);
    }

    @GetMapping("/user/get_user_count")
    JSONObject getUserCount(HttpServletRequest request){
        User currentUser = userService.findCurrentUser(request);
        if (currentUser == null) {
            return ResponseUtil.userNotFind();
        }
        JSONObject ret = new JSONObject();
        ret.put("test_count", practiceService.countExerciseAmountOfUser(currentUser.getId()));
        ret.put("day_count", currentUser.getLogin());
        return ResponseUtil.success("", ret);
    }

    @PostMapping("/user/update_img")
    JSONObject updateUserImg(HttpServletRequest request,
                             @RequestBody JSONObject params){
        User currentUser = userService.findCurrentUser(request);
        currentUser.setImage(params.getString("src"));
        userService.storeUser(currentUser);
        return ResponseUtil.success();
    }

    @PostMapping("/user/update_username")
    JSONObject updateUserName(HttpServletRequest request,
                             @RequestBody JSONObject params){
        User currentUser = userService.findCurrentUser(request);
        currentUser.setName(params.getString("username"));
        userService.storeUser(currentUser);
        return ResponseUtil.success();
    }
}
